package com.zzsy.shoppingpro.community.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.opendanmaku.DanmakuItem;
import com.opendanmaku.DanmakuView;
import com.opendanmaku.IDanmakuItem;
import com.zzsy.shoppingpro.R;
import com.zzsy.shoppingpro.app.Config;
import com.zzsy.shoppingpro.community.bean.NewPostBean;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by Administrator on 2019/8/21 0021.
 */

public class NewPostListViewAdapter extends BaseAdapter {

    private Context mContext;
    private List<NewPostBean> result;
    private List<String> comment_list;

    public NewPostListViewAdapter(Context context, List<NewPostBean> data) {
        this.mContext = context;
        this.result = data;
    }

    @Override
    public int getCount() {
        return result.size();
    }

    @Override
    public Object getItem(int position) {
        return result.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_listview_newpost, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        NewPostBean resultBean = result.get(position);
        holder.tvCommunityUsername.setText(resultBean.getUsername());
        //holder.tvCommunityAddtime.setText();
        Glide.with(mContext)
                .load(Config.BASE_URl_IMAGE + resultBean.getFigure())
                .into(holder.ivCommunityFigure);


        holder.tvCommunitySaying.setText(resultBean.getSaying());
        holder.tvCommunityLikes.setText(resultBean.getLikes());
        holder.tvCommunityComments.setText(resultBean.getComments());

        //设置弹幕
        comment_list = resultBean.getComment_list();
        if (comment_list != null && comment_list.size() > 0) {
            holder.danmakuView.setVisibility(View.VISIBLE);

            List<IDanmakuItem> list = new ArrayList<>();
            for (int i = 0; i < comment_list.size(); i++) {
                IDanmakuItem item = new DanmakuItem(mContext, comment_list.get(i), holder.danmakuView.getWidth());
                list.add(item);
            }

            Collections.shuffle(comment_list);
            holder.danmakuView.addItem(list,true);
            holder.danmakuView.show();
        }else{
            holder.danmakuView.setVisibility(View.GONE);
        }

        return convertView;
    }

    static class ViewHolder {

        TextView tvCommunityUsername;
        TextView tvCommunityAddtime;
        RelativeLayout rl;
        ImageView ivCommunityFigure;
        DanmakuView danmakuView;
        TextView tvCommunitySaying;
        TextView tvCommunityLikes;
        TextView tvCommunityComments;
        ImageButton ibNewPostAvatar;

        public ViewHolder(View convertView) {
            tvCommunityUsername = convertView.findViewById(R.id.tv_community_username);
            tvCommunityAddtime = convertView.findViewById(R.id.tv_community_addtime);
            rl = convertView.findViewById(R.id.rl);
            ivCommunityFigure = convertView.findViewById(R.id.iv_community_figure);
            danmakuView = convertView.findViewById(R.id.danmakuView);
            tvCommunitySaying = convertView.findViewById(R.id.tv_community_saying);
            tvCommunityLikes = convertView.findViewById(R.id.tv_community_likes);
            tvCommunityComments = convertView.findViewById(R.id.tv_community_comments);
            ibNewPostAvatar = convertView.findViewById(R.id.ib_new_post_avatar);

        }
    }
}
